//
//  StoreViewController.m
//  Mtime
//
//  Created by wang xinkai on 15/7/28.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "StoreViewController.h"
#import "HotMovieModel.h"
#import "HotTableViewCell.h"

#import "NewMovieModel.h"
#import "NewTableViewCell.h"

#import "NewTableHeaderView.h"//即将上映列表的头视图




@implementation StoreViewController
{

    UITableView *_hotTableView;//正在热映
    NSMutableArray *_hotDataList;//热映数据
    
    UITableView *_newTableView;//即将上映
    NSMutableDictionary *_newMovieDic;//即将上映数据
    NSMutableArray *_newTableHeaderDateList;//即将上映头部视图的数据
    
    SegmentView *segView;
}


-(id)init{
    
    if (self = [super init]) {
        
        self.title = @"购票";
        self.tabBarItem.image = [UIImage imageNamed:@"payticket"];
        
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_top_movie_background_cover"]];
    }
    return self;
    
}

- (void)changeTableView:(NSInteger)index {

    if (index == 0) {
        
        [UIView animateWithDuration:.35 animations:^{
            
            //使_hotTableView回复初始状态
            _hotTableView.transform = CGAffineTransformIdentity;
            
            _newTableView.transform = CGAffineTransformMakeTranslation(kScreenW, 0);
        }];

    }else if (index == 1) {
            
        [UIView animateWithDuration:.35 animations:^{
            
            //使_newTableView回复初始状态
            _newTableView.transform = CGAffineTransformIdentity;
            
            _hotTableView.transform = CGAffineTransformMakeTranslation(-kScreenW, 0);
        }];

    }
}

- (void)loadSegmentView {

    segView = [[SegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 50) WithTitiles:@[@"正在热映",@"即将上映"]];
    
    //self 在block方法中无法使用  需要使用桥接将之转换成block方法的self
    __block StoreViewController *blockSelf = self;
    [segView addBlcok:^(NSInteger index) {
        
        [blockSelf changeTableView:index];
    }];
    
    segView.selectImageName = @"color_line";
    
    [self.view addSubview:segView];

}

- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self loadSegmentView];
    
    [self loadData];
    
    [self loadTableView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hotMovieAction) name:@"hotMovie" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMovieAction) name:@"newMovie" object:nil];
}

- (void)newMovieAction {

    segView.selectedIndex = 1;
}

- (void)hotMovieAction {

    segView.selectedIndex = 0;

}

- (void)loadData {

    //解析json数据
    /*----------------正在热映数据--------------*/
    _hotDataList = [[NSMutableArray alloc] init];
    
    /*
    NSString *pathFile = [[NSBundle mainBundle] pathForResource:@"buy_now" ofType:@"json"];
    
    NSData *data = [NSData dataWithContentsOfFile:pathFile];
    
    NSDictionary *nowDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    */ //-->最初的json解析方法
    
    NSDictionary *nowDic = [CoreDataSource initWithDataSource:@"buy_now"];
    
    NSArray *ms = [nowDic objectForKey:@"ms"];

    /*
    for (NSDictionary *dic in ms) {
        
        //初始化热映电影model
        HotMovieModel *model = [[HotMovieModel alloc] init];
        //给model属性赋值
     
         @property(nonatomic,copy)NSString *img,*t,*commonSpecial,*rd;
         
         @property(nonatomic,retain)NSNumber *r,*NearestCinemaCount,*NearestShowtimeCount,*is3D,*isIMAX,*isIMAX3D,*isDMAX,*wantedCount;
         //评分  影院  场次  等待
         
         @property(nonatomic,copy)NSArray *versions;
     
        model.img = [dic objectForKey:@"img"];
        model.t = [dic objectForKey:@"t"];
        model.commonSpecial = [dic objectForKey:@"commonSpecial"];
        model.rd = [dic objectForKey:@"rd"];
        model.r = [dic objectForKey:@"r"];
        model.is3D = [dic objectForKey:@"is3D"];
        model.isDMAX = [dic objectForKey:@"isDMAX"];
        model.isIMAX = [dic objectForKey:@"isIMAX"];
        model.NearestCinemaCount = [dic objectForKey:@"NearestCinemaCount"];
        model.NearestShowtimeCount = [dic objectForKey:@"NearestShowtimeCount"];
        model.wantedCount = [dic objectForKey:@"wantedCount"];
        
        [_hotDataList addObject:model];
    }
     */ //-->最初的model赋值方法
    
    for (NSDictionary *dic in ms) {
        
        HotMovieModel *model = [[HotMovieModel alloc] initWithDic:dic];
        
        [_hotDataList addObject:model];
    }
    
    /*---------------即将上映数据--------------*/
    _newMovieDic = [[NSMutableDictionary alloc] init];
    NSDictionary *newDic = [CoreDataSource initWithDataSource:@"buy_new"];
    //电影列表数据
    NSArray *moviecomings = [newDic objectForKey:@"moviecomings"];
    for (NSDictionary *dic in moviecomings) {
        
        NewMovieModel *model = [[NewMovieModel alloc] initWithDic:dic];
        
        NSMutableArray *oldArray = [_newMovieDic objectForKey:[NSString stringWithFormat:@"%@",model.rMonth]];
        
        if (!oldArray) {
            
            NSMutableArray *newArray = [[NSMutableArray alloc] init];
            
            [newArray addObject:model];
            
            [_newMovieDic setValue:newArray forKey:[NSString stringWithFormat:@"%@",model.rMonth]];
        }else {
        
            [oldArray addObject:model];
        }
    }
    
    
    //头部视图数据
    _newTableHeaderDateList = [[NSMutableArray alloc] init];
    
    NSArray *attention = [newDic objectForKey:@"attention"];
    
    for (NSDictionary *dic in attention) {
        
        NewMovieModel *model = [[NewMovieModel alloc] initWithDic:dic];
        
        [_newTableHeaderDateList addObject:model];
    }
    
    
    
}

- (void)loadTableView {

    //正在热映列表
    _hotTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, kScreenW, kScreenH -64 -50)];
    _hotTableView.backgroundColor = [UIColor clearColor];
    _hotTableView.dataSource = self;
    _hotTableView.delegate = self;
    
    [self.view addSubview:_hotTableView];

    
    //即将上映列表
    _newTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, kScreenW, kScreenH -64 -50 -49)];
    _newTableView.transform = CGAffineTransformMakeTranslation(kScreenW, 0);
    _newTableView.delegate = self;
    _newTableView.dataSource = self;
    _newTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_newTableView];
    
    
    //即将上映列表的头视图
    NewTableHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"NewTableHeaderView" owner:nil options:nil] lastObject];
    
    headerView.dataList = _newTableHeaderDateList;
    
    _newTableView.tableHeaderView = headerView;
    
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (tableView == _hotTableView) {
        
        return 1;
    }

    return _newMovieDic.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == _hotTableView) {
        
        return _hotDataList.count;
    }
    
    //获得即将上映列表的对应分组的数据
    NSArray *arr = [[_newMovieDic allValues] objectAtIndex:section];
    
    return arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HotTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"hotCell"];
    if (!cell1) {
        
        cell1 = [[[NSBundle mainBundle] loadNibNamed:@"HotTableViewCell" owner:nil options:nil] lastObject];
    }

    NewTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"newCell"];
    if (!cell2) {
        
        cell2 = [[[NSBundle mainBundle] loadNibNamed:@"NewTableViewCell" owner:nil options:nil] lastObject];
        
    }
    if (tableView == _hotTableView) {
        
        cell1.model = _hotDataList[indexPath.row];
        
        return cell1;

    }else {
    
        //cell所在的分组
        NSArray *array = [[_newMovieDic allValues] objectAtIndex:indexPath.section];
        cell2.model = [array objectAtIndex:indexPath.row];
        
        return cell2;
    }

}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 120;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    if (tableView == _hotTableView) {
        
        return 0;
    }
    
    if (section == 0) {
        return 88;
    }

    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if (tableView == _hotTableView) {
        
        return nil;
    }
    
    
    UIView *headerview = [[UIView alloc] init];
    if (section == 0) {
        
        //第一个分组  头视图高为88
        headerview.frame = CGRectMake(0, 0, kScreenW, 88);
        
        
        //显示电影总是的label
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
        
        NSInteger count = 0;
        
        //显示所有的电影的个数
        for (NSMutableArray *arr in [_newMovieDic allValues]) {
            
            count = count +arr.count;
        }
        
        label1.text = [NSString stringWithFormat:@"即将上映：（%ld部）",count];
        [headerview addSubview:label1];
        
        //显示月份的label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 44, kScreenW, 44)];
        
        label.text = [NSString stringWithFormat:@"%@月",[[_newMovieDic allKeys] objectAtIndex:section]];
        
        [headerview addSubview:label];
    }else {
    
        headerview.frame = CGRectMake(0, 0, kScreenW, 44);
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
        
        label.text = [NSString stringWithFormat:@"%@月",[[_newMovieDic allKeys] objectAtIndex:section]];
        
        [headerview addSubview:label];

    }
    headerview.backgroundColor = [UIColor grayColor];

    return headerview;

}


@end
